package edu.ccut.saturn.studio.dialog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.dialog.validatedialog.MyCellModifierValidate;
import edu.ccut.saturn.studio.dialog.validatedialog.ValidateDialog;
import edu.ccut.saturn.studio.dialog.validatedialog.ValidateLabelProvider;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.ErrorInfo;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnArg;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnValidate;
import edu.ccut.saturn.studio.saturndata.model.ValidateVO;

public class ParentPrimaryDialogForEdit extends Dialog {

	private Button button_1;
	private Button button;
	private Shape shape;

	private Shell shell;

	private TableViewer tableViewer;

	private Table table;

	private Label idLabel, valuekeyLabel, nameLabel, classtypeLabel,
			passingTypeLabel, scopeLabel, descriptionLabel, browser;

	private Text idtext, valuekeytext, nametext, descripiontext;

	private Combo classTypecombo, passingTypecombo, scopecombo;

	protected Object result;

	private Composite compositeTable;

	private Group group;

	private Button addRadioButton, notAddRadioButton, addButton, editButton,
			deleteButton;

	private int idstr;

	private int i = 1;

	private String namestr = Messages.getString("ParentPrimaryDialogForEdit.0"); //$NON-NLS-1$

	private String classTypestr = Messages.getString("ParentPrimaryDialogForEdit.1"); //$NON-NLS-1$

	private String passingTypestr = Messages.getString("ParentPrimaryDialogForEdit.2"); //$NON-NLS-1$

	private String valuekeystr = Messages.getString("ParentPrimaryDialogForEdit.3"); //$NON-NLS-1$

	private String scopestr = Messages.getString("ParentPrimaryDialogForEdit.4"); //$NON-NLS-1$

	private String descriptionstr = "";

	private String isValidate = "";

	private SaturnValidate saturnValide;

	private List<SaturnValidate> validates = new ArrayList<SaturnValidate>();
	private List<SaturnValidate> validatesTemp = new ArrayList<SaturnValidate>();

	private String id = "";

	private String strKey = "";

	private String strName = "";

	private String strClass = "";

	private String strDescription = "";

	public ParentPrimaryDialogForEdit(Shell parent, Shape shape) {
		super(parent);
		this.shell = parent;
		this.shape = shape;
	}

	// 本框打开伊始，validates已有值
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL
				| SWT.RESIZE);
		shell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));

		final GridLayout gridLayout_1 = new GridLayout();
		shell.setLayout(gridLayout_1);
		shell.setSize(500, 540);
		shell.setText(SelectionCandidate.ATTRIBUTE_SETTING);

		Composite compositeButtion;
		Composite compositeProperties = new Composite(shell, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		compositeProperties.setLayout(gridLayout);

		final GridData gd_compositeProperties = new GridData(SWT.LEFT,
				SWT.BOTTOM, false, false);
		gd_compositeProperties.horizontalIndent = -3;
		gd_compositeProperties.widthHint = 471;
		gd_compositeProperties.heightHint = 239;
		compositeProperties.setLayoutData(gd_compositeProperties);

		idLabel = new Label(compositeProperties, SWT.NONE);
		final GridData gd_idLabel = new GridData(SWT.FILL, SWT.FILL, false,
				false);
		gd_idLabel.verticalIndent = 2;
		idLabel.setLayoutData(gd_idLabel);
		idLabel.setText(Messages.getString("ParentPrimaryDialogForEdit.ParaID"));  //$NON-NLS-1$

		idtext = new Text(compositeProperties, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_idtext = new GridData(SWT.FILL, SWT.TOP, true, false);
		gd_idtext.horizontalIndent = 10;
		gd_idtext.horizontalSpan = 2;
		idtext.setLayoutData(gd_idtext);
		idtext.setText(String.valueOf(idstr));

		valuekeyLabel = new Label(compositeProperties, SWT.NONE);
		final GridData gd_valuekeyLabel = new GridData();
		gd_valuekeyLabel.verticalIndent = 5;
		valuekeyLabel.setLayoutData(gd_valuekeyLabel);
		valuekeyLabel.setText(Messages.getString("ParentPrimaryDialogForEdit.Expression"));  //$NON-NLS-1$

		valuekeytext = new Text(compositeProperties, SWT.BORDER);
		valuekeytext.setFocus();
		final GridData gd_valuekeytext = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		gd_valuekeytext.verticalIndent = 3;
		gd_valuekeytext.horizontalIndent = 10;
		valuekeytext.setLayoutData(gd_valuekeytext);
		valuekeytext.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if (e.character == SWT.CR) {
					buttonConfirm();
				}
			}
		});
		valuekeytext.setText(valuekeystr);

		browser = new Label(compositeProperties, SWT.BORDER);
		browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
		browser.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				browser
						.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
			}

			public void mouseExit(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});
		browser.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				FilterDialog dialog1 = new FilterDialog(shell, shape);
				dialog1.setTitle(Messages.getString("ParentPrimaryDialogForEdit.ChooseKeyValue"));  //$NON-NLS-1$
				dialog1.setElements(shape.getParameterValueKeys());
				if (dialog1.open() == Window.OK) {
					String ddd = (String) dialog1.getFirstResult();
					valuekeytext.setText(ddd);
					nametext.setText(ddd);
					classTypecombo.setText(shape.getClassType(ddd));
					passingTypecombo.setText(revertToSurface(shape
							.getPassingType(ddd)));
				}
			}
		});

		nameLabel = new Label(compositeProperties, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.verticalIndent = 5;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("ParentPrimaryDialogForEdit.ParaName"));  //$NON-NLS-1$

		nametext = new Text(compositeProperties, SWT.BORDER);
		final GridData gd_nametext = new GridData(SWT.FILL, SWT.FILL, false,
				false);
		gd_nametext.verticalIndent = 3;
		gd_nametext.horizontalIndent = 10;
		gd_nametext.horizontalSpan = 2;
		nametext.setLayoutData(gd_nametext);
		nametext.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if (e.character == SWT.CR) {
					buttonConfirm();
				}
			}
		});
		nametext.setText(namestr);

		classtypeLabel = new Label(compositeProperties, SWT.NONE);
		final GridData gd_classtypeLabel = new GridData();
		gd_classtypeLabel.verticalIndent = 5;
		classtypeLabel.setLayoutData(gd_classtypeLabel);
		classtypeLabel.setText(Messages.getString("ParentPrimaryDialogForEdit.ParaType"));  //$NON-NLS-1$

		classTypecombo = new Combo(compositeProperties, SWT.READ_ONLY);
		final GridData gd_classTapecombo = new GridData(SWT.FILL, SWT.CENTER,
				false, false);
		gd_classTapecombo.verticalIndent = 3;
		gd_classTapecombo.horizontalIndent = 10;
		gd_classTapecombo.horizontalSpan = 2;
		classTypecombo.setLayoutData(gd_classTapecombo);
		classTypecombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if (e.character == SWT.CR) {
					buttonConfirm();
				}
			}
		});
		classTypecombo.setItems(SelectionCandidate.classtypes);
		classTypecombo.select(0);
		classTypecombo.setText(classTypestr);

		passingTypeLabel = new Label(compositeProperties, SWT.NONE);
		final GridData gd_label = new GridData();
		gd_label.verticalIndent = 5;
		passingTypeLabel.setLayoutData(gd_label);
		passingTypeLabel.setText(Messages.getString("ParentPrimaryDialogForEdit.InputOutput"));  //$NON-NLS-1$

		passingTypecombo = new Combo(compositeProperties, SWT.READ_ONLY);
		final GridData gd_passingTypecombo = new GridData(SWT.FILL, SWT.CENTER,
				false, false);
		gd_passingTypecombo.verticalIndent = 3;
		gd_passingTypecombo.horizontalIndent = 10;
		gd_passingTypecombo.horizontalSpan = 2;
		passingTypecombo.setLayoutData(gd_passingTypecombo);
		passingTypecombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if (e.character == SWT.CR) {
					buttonConfirm();
				}
			}
		});
		passingTypecombo.setItems(SelectionCandidate.passingTypes);
		passingTypecombo.select(0);
		passingTypecombo.setText(passingTypestr);

		scopeLabel = new Label(compositeProperties, SWT.CENTER);
		final GridData gd_scopeLabel = new GridData();
		gd_scopeLabel.verticalIndent = 5;
		scopeLabel.setLayoutData(gd_scopeLabel);
		scopeLabel.setText(Messages.getString("ParentPrimaryDialogForEdit.DataDomain"));  //$NON-NLS-1$

		scopecombo = new Combo(compositeProperties, SWT.READ_ONLY);
		final GridData gd_scopecombo = new GridData(SWT.FILL, SWT.CENTER,
				false, false);
		gd_scopecombo.verticalIndent = 3;
		gd_scopecombo.horizontalIndent = 10;
		gd_scopecombo.horizontalSpan = 2;
		scopecombo.setLayoutData(gd_scopecombo);
		scopecombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if (e.character == SWT.CR) {
					buttonConfirm();
				}
			}
		});
		iniScope(scopecombo);
		scopecombo.setText(getScopestr());

		descriptionLabel = new Label(compositeProperties, SWT.WRAP);
		final GridData gd_descriptionLabel = new GridData(SWT.LEFT, SWT.TOP,
				false, false);
		gd_descriptionLabel.verticalIndent = 5;
		gd_descriptionLabel.widthHint = 80;
		descriptionLabel.setLayoutData(gd_descriptionLabel);
		descriptionLabel.setText(Messages.getString("ParentPrimaryDialogForEdit.ParaDescription"));  //$NON-NLS-1$
		descriptionLabel.pack();

		descripiontext = new Text(compositeProperties, SWT.V_SCROLL
				| SWT.BORDER);
		final FormData fd_descripiontext = new FormData();
		fd_descripiontext.right = new FormAttachment(scopecombo, 0, SWT.RIGHT);
		fd_descripiontext.left = new FormAttachment(20, 0);
		fd_descripiontext.bottom = new FormAttachment(0, 255);
		fd_descripiontext.top = new FormAttachment(descriptionLabel, 0, SWT.TOP);
		descripiontext.setLayoutData(fd_descripiontext);

		final GridData gd_descripiontext = new GridData(SWT.FILL, SWT.CENTER,
				false, false);
		gd_descripiontext.verticalIndent = 3;
		gd_descripiontext.horizontalIndent = 10;
		gd_descripiontext.heightHint = 57;
		gd_descripiontext.horizontalSpan = 2;
		descripiontext.setLayoutData(gd_descripiontext);

		descripiontext.setText(descriptionstr);
		group = new Group(shell, SWT.NONE);
		final GridData gd_group = new GridData();
		gd_group.horizontalIndent = 3;
		group.setLayoutData(gd_group);
		group.setText(Messages.getString("ParentPrimaryDialogForEdit.ApplicationValidate"));  //$NON-NLS-1$
		group.setLayout(new FormLayout());

		addRadioButton = new Button(group, SWT.RADIO);
		addRadioButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				compositeTable.setVisible(true);
				shell.setSize(484, 517);
			}
		});
		final FormData fd_addRadioButton = new FormData();
		fd_addRadioButton.right = new FormAttachment(0, 63);
		fd_addRadioButton.left = new FormAttachment(0, 15);
		addRadioButton.setLayoutData(fd_addRadioButton);
		addRadioButton.setText(Messages.getString("ParentPrimaryDialogForEdit.Yes"));  //$NON-NLS-1$

		notAddRadioButton = new Button(group, SWT.RADIO);
		notAddRadioButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				compositeTable.setVisible(false);
				shell.setSize(484, 400);
			}
		});
		fd_addRadioButton.top = new FormAttachment(notAddRadioButton, 0,
				SWT.TOP);
		final FormData fd_notAddRadioButton = new FormData();
		fd_notAddRadioButton.bottom = new FormAttachment(0, 21);
		fd_notAddRadioButton.top = new FormAttachment(0, 5);
		fd_notAddRadioButton.right = new FormAttachment(0, 150);
		fd_notAddRadioButton.left = new FormAttachment(0, 100);
		notAddRadioButton.setLayoutData(fd_notAddRadioButton);
		notAddRadioButton.setText(Messages.getString("ParentPrimaryDialogForEdit.No"));  //$NON-NLS-1$

		compositeTable = new Composite(shell, SWT.NONE);
		final GridData gd_compositeTable = new GridData(SWT.FILL, SWT.FILL,
				true, true);
		gd_compositeTable.horizontalIndent = 3;
		gd_compositeTable.heightHint = 141;
		compositeTable.setLayoutData(gd_compositeTable);
		compositeTable.setLayout(new FormLayout());

		tableViewer = new TableViewer(compositeTable, SWT.MULTI
				| SWT.FULL_SELECTION | SWT.BORDER);
		table = tableViewer.getTable();
		final FormData fd_table = new FormData();
		fd_table.bottom = new FormAttachment(100, -20);
		fd_table.right = new FormAttachment(100, -50);
		fd_table.top = new FormAttachment(0, 2);
		fd_table.left = new FormAttachment(0, 0);
		table.setLayoutData(fd_table);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		tableViewer.setContentProvider(new TableViewerContentProvider());
		tableViewer.setLabelProvider(new ValidateLabelProvider());

		// 让表格可以直接编辑
		tableViewer.setColumnProperties(new String[] { "id", "key", "name",
				"class", "JS" });

		CellEditor[] cellEditor = new CellEditor[8];
		cellEditor[0] = null;
		cellEditor[1] = new TextCellEditor(tableViewer.getTable());
		cellEditor[2] = new TextCellEditor(tableViewer.getTable());
		cellEditor[3] = new TextCellEditor(tableViewer.getTable());
		cellEditor[4] = new TextCellEditor(tableViewer.getTable());

		tableViewer.setCellEditors(cellEditor);
		tableViewer.setCellModifier(new MyCellModifierValidate(tableViewer));

		final TableColumn newColumnTableColumn = new TableColumn(table,
				SWT.NONE);
		newColumnTableColumn.setWidth(60);
		newColumnTableColumn.setText((Messages.getString("ParentPrimaryDialogForEdit.Number")));   //$NON-NLS-1$

		final TableColumn newColumnTableColumn_1 = new TableColumn(table,
				SWT.NONE);
		newColumnTableColumn_1.setWidth(95);
		newColumnTableColumn_1.setText((Messages.getString("ParentPrimaryDialogForEdit.ID")));  //$NON-NLS-1$

		final TableColumn newColumnTableColumn_2 = new TableColumn(table,
				SWT.NONE);
		newColumnTableColumn_2.setWidth(90);
		newColumnTableColumn_2.setText((Messages.getString("ParentPrimaryDialogForEdit.ValidateName")));  //$NON-NLS-1$

		final TableColumn newColumnTableColumn_3 = new TableColumn(table,
				SWT.NONE);
		newColumnTableColumn_3.setWidth(90);
		newColumnTableColumn_3.setText((Messages.getString("ParentPrimaryDialogForEdit.ValidateClass")));   //$NON-NLS-1$

		final TableColumn newColumnTableColumn_4 = new TableColumn(table,
				SWT.NONE);
		newColumnTableColumn_4.setWidth(90);
		newColumnTableColumn_4.setText((Messages.getString("ParentPrimaryDialogForEdit.JSValidate")));   //$NON-NLS-1$


		addButton = new Button(compositeTable, SWT.NONE);
		addButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				validateAdd();
			}
		});
		final FormData fd_addButton = new FormData();
		fd_addButton.top = new FormAttachment(0, 2);
		fd_addButton.bottom = new FormAttachment(0, 25);
		fd_addButton.left = new FormAttachment(100, -41);
		fd_addButton.right = new FormAttachment(100, -5);
		addButton.setLayoutData(fd_addButton);
		addButton.setText(SelectionCandidate.ADD_BUTTON);

		editButton = new Button(compositeTable, SWT.NONE);
		editButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				validateModify();
			}
		});
		editButton.setText(SelectionCandidate.EDIT_BUTTON);
		final FormData fd_editButton = new FormData();
		fd_editButton.top = new FormAttachment(0, 32);
		fd_editButton.bottom = new FormAttachment(0, 55);
		fd_editButton.right = new FormAttachment(addButton, 0, SWT.RIGHT);
		fd_editButton.left = new FormAttachment(addButton, 0, SWT.LEFT);
		editButton.setLayoutData(fd_editButton);

		deleteButton = new Button(compositeTable, SWT.NONE);
		final FormData fd_deleteButton = new FormData();
		fd_deleteButton.right = new FormAttachment(editButton, 0, SWT.RIGHT);
		fd_deleteButton.left = new FormAttachment(editButton, 0, SWT.LEFT);
		fd_deleteButton.bottom = new FormAttachment(editButton, 31, SWT.BOTTOM);
		fd_deleteButton.top = new FormAttachment(editButton, 8, SWT.BOTTOM);
		deleteButton.setLayoutData(fd_deleteButton);
		deleteButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				validateDelete();
			}
		});
		deleteButton.setText(SelectionCandidate.DELETE_BUTTON);

		button = new Button(compositeTable, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) tableViewer
						.getSelection();
				if (selection.isEmpty()) {
					MessageDialog.openInformation(null,
							SelectionCandidate.HINT,
							SelectionCandidate.CHOOSE_EDIT_RECORD);
				} else {
					SaturnValidate validate = (SaturnValidate) selection
							.getFirstElement();
					List<SaturnValidate> list = (List<SaturnValidate>) tableViewer
							.getInput();
					for (int i = 0; i < list.size(); i++) {
						if (list.get(i).hashCode() == validate.hashCode()) {
							if (i - 1 != -1) {
								list.remove(i);
								list.add(i - 1, validate);
							}
						}
					}
					sortValidates(list);
					tableViewer.setInput(list);
					validatesTemp = list;
				}
			}
		});
		final FormData fd_button = new FormData();
		fd_button.top = new FormAttachment(deleteButton, 5, SWT.BOTTOM);
		fd_button.left = new FormAttachment(deleteButton, 0, SWT.LEFT);
		button.setLayoutData(fd_button);
		button.setText(Messages.getString("ParentPrimaryDialogForEdit.UpShift"));  //$NON-NLS-1$

		button_1 = new Button(compositeTable, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) tableViewer
						.getSelection();
				if (selection.isEmpty()) {
					MessageDialog.openInformation(null,
							SelectionCandidate.HINT,
							SelectionCandidate.CHOOSE_EDIT_RECORD);
				} else {
					SaturnValidate validate = (SaturnValidate) selection
							.getFirstElement();
					List<SaturnValidate> list = (List<SaturnValidate>) tableViewer
							.getInput();
					for (int i = 0; i < list.size(); i++) {
						if (list.get(i).hashCode() == validate.hashCode()) {
							if (i + 1 < list.size()) {
								list.remove(i);
								list.add(i + 1, validate);
								break;
							}
						}
					}
					sortValidates(list);
					tableViewer.setInput(list);
					validatesTemp = list;
				}
			}
		});
		final FormData fd_button_1 = new FormData();
		fd_button_1.top = new FormAttachment(button, 5, SWT.BOTTOM);
		fd_button_1.left = new FormAttachment(button, 0, SWT.LEFT);
		button_1.setLayoutData(fd_button_1);
		button_1.setText(Messages.getString("ParentPrimaryDialogForEdit.DownShift"));  //$NON-NLS-1$
		compositeTable.setTabList(new Control[] { addButton, editButton, table,
				button, button_1 });
		compositeButtion = new Composite(shell, SWT.NONE);
		final GridData gd_compositeButtion = new GridData(SWT.FILL, SWT.FILL,
				false, false);
		gd_compositeButtion.widthHint = 469;
		compositeButtion.setLayoutData(gd_compositeButtion);
		compositeButtion.setLayout(new FormLayout());

		Button buttonYes = new Button(compositeButtion, SWT.NONE);
		buttonYes.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				buttonConfirm();
			}
		});
		final FormData fd_buttonYes = new FormData();
		fd_buttonYes.left = new FormAttachment(0, 305);
		buttonYes.setLayoutData(fd_buttonYes);
		buttonYes.setText("OK");

		Button buttonNo;
		buttonNo = new Button(compositeButtion, SWT.NONE);
		fd_buttonYes.right = new FormAttachment(buttonNo, -5, SWT.LEFT);
		fd_buttonYes.bottom = new FormAttachment(buttonNo, 23, SWT.TOP);
		fd_buttonYes.top = new FormAttachment(buttonNo, 0, SWT.TOP);
		buttonNo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.dispose();
			}
		});
		final FormData fd_buttonNo = new FormData();
		fd_buttonNo.left = new FormAttachment(0, 385);
		fd_buttonNo.right = new FormAttachment(0, 460);
		fd_buttonNo.top = new FormAttachment(buttonYes, 0, SWT.TOP);
		buttonNo.setLayoutData(fd_buttonNo);
		buttonNo.setText("Cancel");
		init();
	}

	private String revertToSurface(String aa) {
		if (aa.equals("in") || aa.equals(SelectionCandidate.passingTypes[0]))
			return SelectionCandidate.passingTypes[0];
		else if (aa.equals("out")
				|| aa.equals(SelectionCandidate.passingTypes[1]))
			return SelectionCandidate.passingTypes[1];
		else if (aa.equals("in-out")
				|| aa.equals(SelectionCandidate.passingTypes[2]))
			return SelectionCandidate.passingTypes[2];
		return "";
	}

	public String getNamestr() {
		return namestr;
	}

	public void setNamestr(String namestr) {
		this.namestr = namestr;
	}

	public String getClassTypestr() {
		return classTypestr;
	}

	private void sortValidates(List<SaturnValidate> list) {
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setId("" + (i + 1));
		}
	}

	public void setClassTypestr(String classTypestr) {
		this.classTypestr = classTypestr;
	}

	public String getPassingTypestr() {
		return passingTypestr;
	}

	public void setPassingTypestr(String passingTypestr) {
		this.passingTypestr = passingTypestr;
	}

	public String getValuekeystr() {
		return valuekeystr;
	}

	public void setValuekeystr(String valuekeystr) {
		this.valuekeystr = valuekeystr;
	}

	public String getScopestr() {
		return scopestr;
	}

	public void setScopestr(String scopestr) {
		this.scopestr = scopestr;
	}

	public String getDescriptionstr() {
		return descriptionstr;
	}

	public void setDescriptionstr(String descriptionstr) {
		this.descriptionstr = descriptionstr;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStrKey() {
		return strKey;
	}

	public void setStrKey(String strKey) {
		this.strKey = strKey;
	}

	public String getStrName() {
		return strName;
	}

	public void setStrName(String strName) {
		this.strName = strName;
	}

	public String getStrClass() {
		return strClass;
	}

	public void setStrClass(String strClass) {
		this.strClass = strClass;
	}

	public List<SaturnValidate> getValidates() {
		return validates;
	}

	public String getStrDescription() {
		return strDescription;
	}

	public void setStrDescription(String strDescription) {
		this.strDescription = strDescription;
	}
	
	private void validateAdd() {
		ValidateVO validateVO = null;
		ValidateDialog vali = new ValidateDialog(shell, validateVO, shape);//打开添加对话框，传vali数组和模型
		vali.open();
		validateVO = vali.getValidateVO();
		if (validateVO == null) {
			return;
		}
		i = this.tableViewer.getTable().getItemCount() + 1;
		tableViewer.setInput(transfer(validates,validateVO));
		valuekeytext.setFocus();
	}
	
	@SuppressWarnings("unchecked")
	private List<SaturnValidate> transfer(List<SaturnValidate> validates,
			ValidateVO validateVO) {
		SaturnValidate volists = new SaturnValidate();
		List<SaturnArg> args = new ArrayList<SaturnArg>();
		List<ErrorInfo> errs = new ArrayList<ErrorInfo>();
		
		
		volists.setId(String.valueOf(i++));
		volists.setKey(validateVO.getKey());
		volists.setName(validateVO.getName());
		volists.setValidateClass(validateVO.getClassTypeStr());
		volists.setJsMethodName(validateVO.getJSMethodName());
		volists.setArgs(transferArgs(validateVO.getArgs(), args));
		volists.setDescription(validateVO.getDescription());
		volists.setEventInfos(validateVO.getEventInfos());
		volists.setValidateType(validateVO.getValidateType());
		volists.setErrorInfos(transferErrs(validateVO.getErrorInfos(),errs));
		validatesTemp.add(volists);

		return this.validates = validatesTemp;
	}
		
	private List<ErrorInfo> transferErrs (Map<String, String> errs1, List<ErrorInfo> errs2) {
		int i = 0;
		for(; i < errs1.size(); i++) {
			ErrorInfo err = new ErrorInfo();
			err.setKey((String) errs1.keySet().toArray()[i]);
			err.setValue((String) errs1.values().toArray()[i]);
			errs2.add(err);
		}
		return errs2;
	}
	
	private List<SaturnArg> transferArgs (List<String> args1, List<SaturnArg> args2) {
		int i = 0;
		
		for (String arg : args1) {
				SaturnArg arg1 = new SaturnArg();
					arg1.setArg(arg);
					arg1.setId(String.valueOf(i++));
					args2.add(arg1);
		}
		return args2;
	}
		
	private void validateModify() {
		IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
		if (selection.isEmpty()) {
			MessageDialog.openInformation(null, SelectionCandidate.HINT , SelectionCandidate.CHOOSE_EDIT_RECORD);
		} else {
			ValidateVO validateVO = null;
			int j = tableViewer.getTable().getSelectionIndex();
			i = ++j;
			ValidateDialog vali = new ValidateDialog(shell, validateVO, shape);
			vali.open();
			validateVO = vali.getValidateVO();
			validates.remove(j - 1);
			validatesTemp.remove(j - 1);
			validates = transfer(validates,validateVO);
			this.reorder(validates);
			tableViewer.setInput(validates);
			i = tableViewer.getTable().getItemCount() + 1;
			valuekeytext.setFocus();
		}
	}

	private void validateDelete() {
		IStructuredSelection selection = (IStructuredSelection) tableViewer
				.getSelection();
		if (selection.isEmpty()) {
			MessageDialog.openInformation(null, SelectionCandidate.HINT, SelectionCandidate.CHOOSE_DELETE_RECORD);
		} else {
			validates.remove(tableViewer.getTable().getSelectionIndex());
			this.validatesTemp = validates;
			this.reorder(validates);
			tableViewer.setInput(validates);
			i = tableViewer.getTable().getItemCount() + 1;
			valuekeytext.setFocus();
		}
	}

	private void reorder(List<SaturnValidate> parameters) {

		if (parameters != null) {
			Collections.sort(parameters);
			for (int i = 0; i < parameters.size(); i++) {
				parameters.get(i).setId("" + (i + 1));
			}
		}
	}

	private void init() {
		setSaturnValidatelist(validates);// 初始化时把validates赋给validatesTemp
		if (this.getIsValidate() != null) {
			if (this.getIsValidate().equals("true")) {
				addRadioButton.setSelection(true);
				tableViewer.setInput(validatesTemp);
			} else if (this.getIsValidate().equals("false")) {
				shell.setSize(484, 400);
				notAddRadioButton.setSelection(true);
				compositeTable.setVisible(false);
				tableViewer.setInput(validatesTemp);
			}
//			else {
//			 addRadioButton.setSelection(true);
//			 tableViewer.setInput(validates);
//			 }
		}
	}

	private void buttonConfirm() {
		i = 1;
		namestr = nametext.getText();
		classTypestr = classTypecombo.getText();
		passingTypestr = passingTypecombo.getText();
		valuekeystr = valuekeytext.getText();
		scopestr = scopecombo.getText();
		descriptionstr = descripiontext.getText();
		setValidates(validatesTemp);
		if (addRadioButton.getSelection()) {
			setIsValidate("true");
		} else if (notAddRadioButton.getSelection()) {
			setIsValidate("false");
		}
		shell.dispose();
	}

	// 复制一份validates，令其为validatesTemp
	public void setSaturnValidatelist(List<SaturnValidate> saturnValidateList) {
		List<SaturnValidate> showParameterlist = new ArrayList<SaturnValidate>();
		for (SaturnValidate validates : saturnValidateList) {
			showParameterlist.add(new SaturnValidate(validates));
		}
		this.validatesTemp = showParameterlist;
	}

	public String getIsValidate() {
		return isValidate;
	}

	public void setIsValidate(String isValidate) {
		this.isValidate = isValidate;
	}

	public int getIdstr() {
		return idstr;
	}

	public void setIdstr(int idstr) {
		this.idstr = idstr;
	}

	public void setValidates(List<SaturnValidate> validates) {
		this.validates = validates;
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	protected void iniScope(Combo combo) {
		scopecombo.setItems(SelectionCandidate.scopes);
	}
}
